package com.amphora.services.application.impl;

import java.security.Key;
import java.util.Calendar;
import java.util.Properties;

import javax.crypto.Cipher;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.joda.time.DateTime;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import com.amphora.services.application.UtilService;
import com.amphora.util.AESCrypto;

public class UtilServiceImpl implements UtilService{
	private AESCrypto crypto = new AESCrypto();
	
	public DateTime convertCalendarToDateTime(Calendar date){
		return new DateTime(date);
	}

	public DateTime calculateEndOfCalendarEventDT(DateTime start, int duration){
		DateTime end = new DateTime();
		end = start.plusMinutes(duration);
		
		return end;
	}
	
	public Calendar calculateEndOfCalendarEventCal(Long start, int duration){
		Calendar end = Calendar.getInstance();
		end.setTimeInMillis(start);
		end.add(Calendar.MINUTE, duration);
		return end;
	}

	@Override
	public boolean sendNotificationEmail(String to, String subject,String body) {
		  String from = "noreply.amphora@gmail.com";
		  final String username = "noreply.amphora";
		  final String password = "amphora2013";
	      
	      
	      Properties props = new Properties();
	      props.put("mail.smtp.auth", "true");
	      props.put("mail.smtp.starttls.enable", "true");
	      props.put("mail.smtp.host", "smtp.gmail.com");
	      props.put("mail.smtp.port", "587");
	      
	      Session session = Session.getInstance(props, new javax.mail.Authenticator(){
	    	 protected PasswordAuthentication getPasswordAuthentication(){
	    		 return new PasswordAuthentication(username, password);
	    	 }
	      });
	      
	      try{
	    	  Message confirmationEmail = new MimeMessage(session);
	    	  
	    	  confirmationEmail.setFrom(new InternetAddress(from));
	    	  
	    	  confirmationEmail.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
	    	  
	    	  confirmationEmail.setRecipient(Message.RecipientType.BCC, new InternetAddress(from));
	    	  
	    	  confirmationEmail.setSubject("Confirmation Of Registration For: "+subject);
	    	  
	    	  confirmationEmail.setText(body);
	    	  
	    	  Transport.send(confirmationEmail);
	    	  
	    	  return true;
	      }catch(Exception e){
	    	  e.printStackTrace();
	    	  return false;
	      }
	}
	
	public String encryptPassword(String password) throws Exception {
		Key key = crypto.generateKey(); 
		Cipher c = Cipher.getInstance(crypto.getALGORITHM()); 
		c.init(Cipher.ENCRYPT_MODE, key); 
		byte[] encValue = c.doFinal(password.getBytes()); 
		String encryptedValue = new BASE64Encoder().encode(encValue); 
		return encryptedValue;
	}

	public String decrypt(String encryptedValue) throws Exception { 
		Key key = crypto.generateKey(); 
		Cipher c = Cipher.getInstance(crypto.getALGORITHM()); 
		c.init(Cipher.DECRYPT_MODE, key); 
		byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedValue); 
		byte[] decValue = c.doFinal(decordedValue); 
		String decryptedValue = new String(decValue); 
		return decryptedValue; 
	} 
}
